require "crystagiri"
require "http/client"
require "uri"

module Lyristal
  VERSION = "0.1.0"

  class AZLyrics
    def getLyricsPage(artist, title)
      cleanedArtist = artist.gsub(/[^A-Za-z0-9]/, "").downcase
      cleanedTitle = title.gsub(/[^A-Za-z]/, "").downcase
      lyricsURL = "https://www.azlyrics.com/lyrics/#{cleanedArtist}/#{cleanedTitle}.html"

      HTTP::Client.get lyricsURL do |response|
        return {
          "code": response.status_code,
          "url": lyricsURL
        }
      end
    end

    def scrapeLyrics(url)
      html = HTTP::Client.get url
      doc = Crystagiri::HTML.new html.body

      doc.where_tag("div") do |tag|
        if !tag.classname && tag.node.to_s.includes? "<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->"
          return tag.content.to_s.lstrip
        end
      end
    end
  end
end